package com.gxust.controller;


import com.gxust.dao.OrderDao;
import com.gxust.util.jwt.GetData;
import com.gxust.util.result.Result;
import com.gxust.vo.OrderSellerVO;
import com.gxust.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单管理
 */
@Slf4j
@RestController
@Api(tags = "订单管理")
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private com.gxust.service.OrderService OrderService;

    /**
     * 添加订单
     */
    @PostMapping("/add")
    @ApiOperation("add order")
    public Result addOrder(@RequestBody OrderDao orderDao, HttpServletRequest request) {
        log.info("add order");
        // 获取用户ID
        Integer userId = GetData.getUserId(request);
        orderDao.setUserId(userId);
        // 从requestData中获取list和address
        // 调用service函数，传递订单信息列表
        OrderService.addOrders(orderDao);
        return Result.success("Orders added successfully");
    }

    @GetMapping("/getOrder")
    @ApiOperation("根据用户id查询订单数据")
    public Result getOrders(HttpServletRequest request){
        Integer userId = GetData.getUserId(request);
        List<OrderVO> list =  OrderService.getOrders(userId);
        return Result.success(list);
    }

    @GetMapping("/seller/getOrder")
    @ApiOperation("商家查询订单数据")
    public Result getOrdersSeller(HttpServletRequest request){
        Integer id = GetData.getUserId(request);
        List<OrderSellerVO> list = OrderService.getOrdersBySellerId2(id);
        return Result.success(list);
    }

}

